require "import"
import "str"

local data={}

local isProduction=this.getPackageName():find"and"==nil

if isProduction then

local json=require"cjson"
local config=require"widget_config/oneword"
:new(this)

local isAutoDark=config.isAutoDark()

local ids=this.getSystemService("appwidget")
        .getAppWidgetIds(luajava.bindClass"android.content.ComponentName"(this,"com.windmill.widget.OneWordWidgetProvider"))

for k=0,#ids-1 do
local id=ids[k]
local style=config:getStyle(id)
local sen=config:getSentence(id)
local textSize=config:getTextSize(id)
local isVip=activity.getSharedData("vip_time")~=nil and activity.getSharedData("vip_time")~="已到期" and activity.getSharedData("vip_time")~="未开通"
local d={__type=1,
title={},
source={},
background={
foreground=波纹(波纹色,圆角(nil,-1,{20,20,20,20,20,20,20,20})),
onClick=function()
--this.setDebug(true)
this.startActivity(
Intent()
.setClass(this,this.getClass())
.setData(luajava.bindClass"android.net.Uri".parse(this.getLuaDir().."/widget/settings.lua"))
.putExtra("appWidgetId",int(id))
)
end
},
}
if sen==""||!isVip then
d.title.text=isVip
&&"你若安好，便是晴天！"
||"成为会员即可查看"
d.source.visibility=8
d.source.text=nil
else
local sentence=json.decode(sen)
d.title.text=sentence.hitokoto
d.source.visibility=0
d.source.text=sentence.from
end
d.title.textSize=textSize--.."px"
d.source.textSize=textSize-2-- .."px"
if style==0 then
d.title.textColor=isAutoDark&&文字色||0xff000000
d.source.textColor=d.title.textColor
d.background.background=
isAutoDark&&ColorDrawable(0)
||圆角(nil,-1,{20,20,20,20,20,20,20,20})
elseif style==1 then
d.title.textColor=isAutoDark&&文字色||-1
d.source.textColor=d.title.textColor
d.background.background=
isAutoDark&&ColorDrawable(0)
||圆角(nil,0xff000000,{20,20,20,20,20,20,20,20})
else
d.title.textColor=文字色
d.source.textColor=d.title.textColor
d.background.background=ColorDrawable(0)
end
table.insert(data,d)
end

end


local adp=LuaMultiAdapter(this,data,{
{--1
FrameLayout,
padding="16dp",
clickable=true,
layout_width=-1,
clickable=true,
{
LinearLayout,
layout_width=-1,
padding="10dp",
orientation=1,
gravity=17,
--minimumHeight="156dp",
id="background",
{
TextView,
layout_gravity=17,
id="title",
},
{
TextView,
layout_gravity=5,
layout_marginTop="4dp",
id="source",
}
},
},
})

this.setContentView(loadlayout ({
  RelativeLayout,
  --layout_width="fill",
  --layout_height="fill",
  --visibility=4,
  backgroundColor=背景色,
  --orientation="vertical",
  --elevation="2%w",
  paddingTop=状态栏高度,
  {
    RelativeLayout,
    layout_width="fill",
    layout_height="56dp" ,
    gravity="center",
    padding="16dp",
    --backgroundColor=0xffffffff,
    elevation="2dp",
    paddingTop="8dp",
    paddingBottom="8dp",
    onClick=function()
      if topbar_click+2 > tonumber(os.time()) then
        list.smoothScrollToPosition(0,0)
       else
        topbar_click=tonumber(os.time())
      end
    end;
    {
      ImageView,
      src="drawable/back.png",
      layout_height="fill",
      layout_width="44dp",
      foreground=波纹(波纹色),
      onClick=function ()
        this.finish()
      end,
      padding="9dp",
      colorFilter=图标色,
    },
    
    {
      TextView,
      layout_width="fill",
      layout_height="fill",
      gravity="center",
      --layout_gravity="center",
      text="微件设置",
      textSize="20sp",
      textColor=文字色,
    },
  },
  
  {
  TextView,
  id="hint",
  layout_width=-1,
      layout_height=-1,
      paddingTop="56dp",
      visibility=8,
    gravity=17
  },
    {
      ListView,
      dividerHeight=0,
      layout_width=-1,
      layout_height=-1,
      paddingTop="56dp",
      clipToPadding=false,
      id="list",
      adapter=adp,
      
      
    },
  
  {
    LinearLayout,
    layout_width="fill",
    layout_height="56dp" ,
    foreground=上下渐变({背景色,深透,淡透}),
  },
}))

if adp.count==0 then
hint.visibility=0
list.visibility=8
hint.text=isProduction&&"没有微件\
长按桌面添加微件"
||"请构建后使用"
end
